const mongoose = require('mongoose');
mongoose.connect("mongodb://localhost/playground",{useUnifiedTopology: true,useNewUrlParser: true})
  .then(()=>{console.log('数据库连接成功')})
  .catch(err=>{console.log('数据库连接失败')})

//创建集合规则
let courseSchema = new mongoose.Schema({
  name:String,
  author:String,
  isPublished:Boolean
})

//使用规则创建集合
const Course = mongoose.model('Course',courseSchema)  //courses
// //向集合插入文档
// Course.create({name:'JavaScript',author:"xiaduanjian",isPublished:false},(err,result)=>{
//   console.log(err);
//   console.log(result);
// })
Course.create({name:'html',author:"xiaduanjian",isPublished:false})
  .then(result=>{
    console.log(result)
  })
  .catch(err=>{
    console.log(err)
  })